/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager.edit;

import br.com.foxline.enums.CivilStatus;
import br.com.foxline.enums.Gender;
import br.com.foxline.enums.TypeAddress;
import br.com.foxline.model.Address;
import br.com.foxline.model.Annex;
import br.com.foxline.model.City;
import br.com.foxline.model.Customer;
import br.com.foxline.service.CityService;
import br.com.foxline.service.CustomerService;
import br.com.foxline.util.Caracter;
import br.com.foxline.util.Msg;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerEditCustomer implements Serializable {

    @EJB
    CustomerService customerService;
    @EJB
    CityService cityService;
    private Customer customer;
    private Customer conjugue;
    private Address address;
    private List<Address> addresses;
    private City city;
    private List<Annex> annexs;
    
    @PostConstruct
    public void init() {
        address = new Address();
    }

    public List<SelectItem> getGender() {
        List<SelectItem> genders = new ArrayList<SelectItem>();
        for (Gender ms : Gender.values()) {
            genders.add(new SelectItem(ms, ms.getName()));
        }
        return genders;
    }

    public List<SelectItem> getCivilStatus() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (CivilStatus c : CivilStatus.values()) {
            items.add(new SelectItem(c, c.getName()));
        }
        return items;
    }

    public List<SelectItem> getTypeAddress() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (TypeAddress c : TypeAddress.values()) {
            items.add(new SelectItem(c, c.getName()));
        }
        return items;
    }

    public void update() {
        try {

            if (addresses.size() > 0) {
                if (typeCStatus()) {
                    customer.setSpouse(conjugue);
                    customerService.saveCustomer(customer);
                    init();
                } else {
                    customer.setCelPhone(Caracter.removecaracter(customer.getCelPhone()));
                    customer.setFixedPhone(Caracter.removecaracter(customer.getFixedPhone()));
                    customer.setCpf(Caracter.removecaracter(customer.getCpf()));
                    customer.setAddresses(addresses);
                    customer.setAnnexs(annexs);
                    customerService.save(customer);
                    Msg.messagemInfo("Cliente Cadastrado com Sucesso");
                    init();
                }
            } else {
                Msg.messagemWarn("O Cliente deve possuir um endereço");
            }

        } catch (Exception e) {
            Msg.messagemError("Erro ao Salvar Cliente.");
        }


    }

    public boolean typeCStatus() {
        boolean b;
        if (customer.getCivilStatus() == CivilStatus.CASADO) {
            b = true;
        } else {
            b = false;
        }
        return b;
    }

    public void addAddress() {
        address.setCity(city);
        address.setCep(Caracter.removecaracter(address.getCep()));
        addresses.add(address);
        address = new Address();
    }

    public void edit(Address ad, int id) {
        System.out.println(ad);
        address = ad;
        city = cityService.find(address.getCity().getId());
        addresses.remove(id);

    }

    public void remove(int id) {
        addresses.remove(id);
    }

    public void pageEditCustomer(String s, Customer c) {
        this.customer = customerService.find(c.getId());
        this.addresses = customerService.findAddresses(this.customer);
        this.conjugue = this.customer.getSpouse() == null ? new Customer() : this.customer.getSpouse();
        
        redirect(s, c);
    }

    public void redirect(String string, Object o) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerEditCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void customerExist() {
        conjugue.setCpf(Caracter.removecaracter(conjugue.getCpf()));
        Customer c = customerService.existCustomer(conjugue);
        if (c != null) {
            conjugue = customerService.find(c.getId());
        }
        if (c == null) {
        }

    }

    public void cleanAddress() {
        this.address = new Address();
    }

    public List<City> autocompleteCity(String query) {
        return cityService.autocomplete(query);
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Customer getConjugue() {
        return conjugue;
    }

    public void setConjugue(Customer conjugue) {
        this.conjugue = conjugue;
    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }
}
